// 系统商品Datatable
var prjSkuTable;

// checkbox数组
var chkGoodsArry = new Array();
/**
 * 页面初始化
 */
$(function() {
    // 绑定查询按钮click
    $("#btnPrjSkuSearch").click(function() {
        doQuery();
    });

    // 全选，全不选商品
    var checkboxes = window.top.window.document.getElementsByName('chkItem');
    $("#chkAll").click(function() {
        for (var i = 0; i < checkboxes.length; i++) {
            var checkbox = checkboxes[i];
            if (!$(this).get(0).checked) {
                checkbox.checked = false;
            } else {
                checkbox.checked = true;
            }
        }
    });
});

/**
 * 查询goodsSku
 * 
 * @returns
 */
function doQuery() {
    var goodsCode = $('#skuGoodsCode', window.top.document).val().trim();
    var goodsName = $('#skuGoodsName', window.top.document).val().trim();
    var skuCode = $('#skuSkuCode', window.top.document).val().trim();
    var skuName = $('#skuSkuName', window.top.document).val().trim();
    if (goodsCode == "" && goodsName == "" && skuCode == "" && skuName == "") {
        window.top.window.showModalAlert("请选择一个检索条件");
        return;
    }

    // 检索商品选择列表
    prjSkuTable = $('#tblSelPrjSku', window.top.document).DataTable({
        "paging" : false,
        "processing" : true,
        "serverSide" : true,
        "dom" : '<"bottom"rtflp>',
        "searching" : false,
        "deferRender" : true,
        "ajax" : {
            "url" : "/stockIn/getPrjSkuList.action",
            "data" : function(d) {
                var advData = {
                    "goodsCode" : encodeURI($('#skuGoodsCode', window.top.document).val().trim()),
                    "goodsName" : encodeURI($('#skuGoodsName', window.top.document).val().trim()),
                    "skuCode" : encodeURI($('#skuSkuCode', window.top.document).val().trim()),
                    "skuName" : encodeURI($('#skuSkuName', window.top.document).val().trim())
                };
                d.formJson = JSON.stringify(advData);
            }
        },
        "fnDrawCallback" : function(oSettings) {
            $("#thSelPrjSku", window.top.document).removeClass("sorting_asc");
        },
        "tableTools" : {
            "sRowSelect" : "bootstrap"
        },
        "columns" : [ {
            "data" : "skuCode",
            "render" : function(data, type, row, meta) {
                return '<input type="checkbox" name="chkItem" value="" />';
            },
            "orderable" : false
        }, {
            "data" : "goodsCode",
            "orderable" : false
        }, {
            "data" : "goodsName",
            "orderable" : false
        }, {
            "data" : "skuCode",
            "orderable" : false
        }, {
            "data" : "skuName",
            "orderable" : false
        }, {
            "data" : "salePrice",
            "orderable" : false
        }, {
            "data" : "costPrice",
            "orderable" : false
        }, {
            "data" : "isGifts",
            "render" : function(data, type, row, meta) {
                if (row.isGifts == "y") {
                    return '是';
                } else {
                    return '否';
                }
            },
            "orderable" : false
        }, {
            "data" : "isVirt",
            "render" : function(data, type, row, meta) {
                if (row.isVirt == "y") {
                    return '是';
                } else {
                    return '否';
                }
            },
            "orderable" : false
        }, {
            "data" : "goodsId",
            "visible" : false
        }, {
            "data" : "skuId",
            "visible" : false
        }, {
            "data" : "stanPurcPrice",
            "visible" : false
        } ]
    });
}

/**
 * 获取选择的sku列表
 */
function getSelectedSKU() {
    //返回值
    var selectedSkuArray = [];
    
    // 列表上选择的sku
    $("#tblSelPrjSku tbody input[type='checkbox']:checked", window.top.document).each(function(k, v) {
        var trNode = this.parentNode.parentNode;
        var rowData = prjSkuTable.row(trNode).data();
        var aSku = {};
        aSku.goodsCode = rowData.goodsCode;
        aSku.goodsName = rowData.goodsName;
        aSku.skuCode = rowData.skuCode;
        aSku.skuName = rowData.skuName;
        aSku.goodsId = rowData.goodsId;
        aSku.skuId = rowData.skuId;
        aSku.stanPurcPrice = rowData.stanPurcPrice;
        aSku.salePrice = rowData.salePrice;
        aSku.barCode = rowData.barCode;
        aSku.costPrice = rowData.costPrice;
        aSku.isGifts = rowData.isGifts;
        aSku.isVirt = rowData.isVirt;
        selectedSkuArray.push(aSku);
    });
    return selectedSkuArray;
}
